package com.angis.fx.util;

import java.util.ArrayList;
import java.util.List;

import com.angis.fx.data.ContextInfo;
import com.angis.fx.data.DepartmentInfo;
import com.angis.fx.data.OrganizationInfo;
import com.angis.fx.data.UserInfo;

public class LoginParseUtil {

	public static String TESTSOURCE = "1&AT00005&0411291651049911$浙江省文化厅#" +
			"厅领导=1050324081533345:yjx=杨$sct=沈$qyw=齐$bb=鲍;厅办公室=1050712175639796:lhe=河$ni=倪$by=包;" +
			"人事处=1050714145902396:;" +
			"系统管理=1050610150713765:zhh=海宏$ldz=李中$xz=徐周$lw=刘伟;" +
			"#0411291651049911";
	private static ContextInfo mContextInfo;
	
	public static ContextInfo onlineParse(String pSource) {
		mContextInfo = new ContextInfo();
		String lRes[] = pSource.split("&amp;");
		//登录标识
		mContextInfo.setLoginFlag(Integer.valueOf(lRes[0]));
		//设备编号
		mContextInfo.setDeviceno(lRes[1]);
		String lRes2[] = lRes[2].split("#");
		parseOrganizationInfoInfo(lRes2[0]);
		String lDepartmentInfo = lRes2[1].replace(" ", "");
		parseDepartment(lDepartmentInfo);
		String lDpLevel = lRes2[2];
		StringBuffer lMemberInfo = new StringBuffer();
		lMemberInfo.append(lRes[2]);
		mContextInfo.setMemberInfos(lMemberInfo.toString());
		mContextInfo.setDpLevel(lDpLevel);
		return mContextInfo;
	}
	
	public static ContextInfo outlineParse(String pSource) {
		mContextInfo = new ContextInfo();
		//登录标识
		mContextInfo.setLoginFlag(2);
		String lRes[] = pSource.split("#");
		parseOrganizationInfoInfo(lRes[0]);
		String lDepartmentInfo = lRes[1].replace(" ", "");
		parseDepartment(lDepartmentInfo);
		String lDpLevel = lRes[2];
		mContextInfo.setMemberInfos(pSource);
		mContextInfo.setDpLevel(lDpLevel);
		return mContextInfo;
	}
	
	private static void parseOrganizationInfoInfo(String pDeviceInfo) {
		String lOrganlizationInfo[] = pDeviceInfo.split("\\$");
		
		OrganizationInfo lOrgInfo = new OrganizationInfo();
		//单位编号
		lOrgInfo.setOid(lOrganlizationInfo[0]);
		//单位名称
		lOrgInfo.setName(lOrganlizationInfo[1]);
		mContextInfo.setOrganizationInfo(lOrgInfo);
		
	}
	
	private static void parseDepartment(String pDepartmentInfo) {
		String lDpInfos[] = pDepartmentInfo.split(";");
		if(null != lDpInfos) {
			List<DepartmentInfo> lDepartmentInfoList = new ArrayList<DepartmentInfo>();
			for (String dpInfo : lDpInfos) {
				String lDps[] = dpInfo.split(":");
				DepartmentInfo lDpInfo = new DepartmentInfo();
				lDpInfo.setName(lDps[0].split("=")[0]);
				lDpInfo.setPid(lDps[0].split("=")[1]);
				if(lDps.length > 1) {
					parseUserInfo(lDpInfo, lDps[1]);
				}
				lDepartmentInfoList.add(lDpInfo);
			}
			mContextInfo.getOrganizationInfo().setDpmentInfos(lDepartmentInfoList);
		}
	}
	
	private static void parseUserInfo(DepartmentInfo pDpInfo, String pUserInfo) {
		String lUserInfos[] = pUserInfo.split("\\$");
		if(null != lUserInfos) {
			List<UserInfo> lUserInfoList = new ArrayList<UserInfo>();
			for (String userInfo : lUserInfos) {
				UserInfo lUserInfo = new UserInfo();
				lUserInfo.setUid(userInfo.split("=")[0]);
				lUserInfo.setName(userInfo.split("=")[1]);
				lUserInfoList.add(lUserInfo);
			}
			if(!lUserInfoList.isEmpty()) {
				pDpInfo.setUserInfos(lUserInfoList);
			}
		}
	}
	
	public static void main(String[] args) {
		ContextInfo lContextInfo = onlineParse(TESTSOURCE);
		System.out.println(lContextInfo);
	}

}
